﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Data;
using LotteryAdmin.AdminService;
namespace LotteryAdmin
{
    public partial class MemberInfo : UserControl
    {
        AdminService.LotteryAdminClient client;
        private int currentPos = 0;
        private List<int> itemCount = new List<int>();
        public MemberInfo()
        {
            InitializeComponent();
            client = WcfProxy.GetProxy;
            client.GetMemberListDataCompleted += new EventHandler<AdminService.GetMemberListDataCompletedEventArgs>(client_GetMemberListDataCompleted);
            //client.GetMemberListDataCountCompleted += new EventHandler<AdminService.GetMemberListDataCountCompletedEventArgs>(client_GetMemberListDataCountCompleted);
            client.GetLowerMemberByUIDCompleted += new EventHandler<GetLowerMemberByUIDCompletedEventArgs>(client_GetLowerMemberByUIDCompleted);
            this.KeyDown += new KeyEventHandler(Page_KeyDown);
        }
        void Page_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.PlatformKeyCode == 13)
                this.btnSearch_Click(this.btnSearch, null);
        }
        //private void client_GetMemberListDataCountCompleted(object sender, AdminService.GetMemberListDataCountCompletedEventArgs e)
        //{
        //    itemCount.Clear();
        //    if (DocPanelData.Children.Count==2)
        //    {
        //        DocPanelData.Children.RemoveAt(1);
        //    }
        //    dpList = new DataPager();
        //    dpList.PageSize = 15;
        //    dpList.PageIndexChanged += new EventHandler<EventArgs>(dpList_PageIndexChanged);
        //    DocPanelData.Children.Add(dpList);
        //    for (int i = 0; i < e.Result; i++)
        //    {
        //        itemCount.Add(i);
        //    }
        //    PagedCollectionView pcv = new PagedCollectionView(itemCount);
        //    pcv.PageSize = 1;
        //    dpList.Source = pcv;
        //}

        private void client_GetMemberListDataCompleted(object sender, AdminService.GetMemberListDataCompletedEventArgs e)
        {
            loadingProgress.IsBusy = false;
            currentPos = 0;
            if (e.Result.Count == 0)
            {
                MessageBox.Show("没有找到匹配的记录!");
            }
            PagedCollectionView pageView = new PagedCollectionView(e.Result);
            dataPager.Source = pageView;
            dgList.ItemsSource = pageView;
        }

        private void btnSearch_Click(object sender, RoutedEventArgs e)
        {
            sp_Navigater.Children.Clear();
            loadingProgress.IsBusy = true;
            client.GetMemberListDataAsync(txtUserID.Text.Trim(), cbxOnline.SelectedIndex, cbxLock.SelectedIndex, cbxType.SelectedIndex,txtUID.Text.Trim());
        }

        private void btChange_Click(object sender, RoutedEventArgs e)
        {
            Button btn = sender as Button;
            ChildModifyMemberInfo dmm = new ChildModifyMemberInfo();
            dmm.showChildWindow(btn.Tag as AdminService.MemberList);
            dmm.Closed += (s, er) =>
            {
                if (dmm.isHaveChanged == true)
                {
                    if ((btn.Tag as AdminService.MemberList).IsActive == 0)
                    {
                        (dgList.Columns[13].GetCellContent((btn.Tag as AdminService.MemberList)) as Button).Content = "锁定";
                        (dgList.Columns[13].GetCellContent((btn.Tag as AdminService.MemberList)) as Button).Foreground = new SolidColorBrush(Colors.Red);
                    }
                    else
                    {
                        (dgList.Columns[13].GetCellContent((btn.Tag as AdminService.MemberList)) as Button).Content = "激活";
                        (dgList.Columns[13].GetCellContent((btn.Tag as AdminService.MemberList)) as Button).Foreground = new SolidColorBrush(Colors.Green);
                    }
                }
            };
        }

        private void btView_Click(object sender, RoutedEventArgs e)
        {
            //Button btn = sender as Button;
            //new ChildModifyMemberInfo(btn.Tag.ToString(),true).Show();
        }

        private void dgList_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            currentPos += 1;
            (dgList.Columns[0].GetCellContent(e.Row) as TextBlock).Text = (dataPager.PageIndex * dataPager.PageSize + e.Row.GetIndex() + 1).ToString();
            if ((e.Row.DataContext as AdminService.MemberList).LastLoginTime == DateTime.MinValue)
            {
                (dgList.Columns[5].GetCellContent(e.Row) as TextBlock).Visibility = Visibility.Collapsed;
            }
            else
            {
                (dgList.Columns[5].GetCellContent(e.Row) as TextBlock).Visibility = Visibility.Visible;
            }
            if ((e.Row.DataContext as AdminService.MemberList).IsActive == 0)
            {
                (dgList.Columns[13].GetCellContent(e.Row) as Button).Content = "锁定";
                (dgList.Columns[13].GetCellContent(e.Row) as Button).Foreground = new SolidColorBrush(Colors.Red);
            }
            else
            {
                (dgList.Columns[13].GetCellContent(e.Row) as Button).Content = "激活";
                (dgList.Columns[13].GetCellContent(e.Row) as Button).Foreground = new SolidColorBrush(Colors.Green);
            }
        }

        //private void dpList_PageIndexChanged(object sender, EventArgs e)
        //{
        //    client.GetMemberListDataAsync(txtUserID.Text.Trim(), cbxOnline.SelectedIndex, cbxLock.SelectedIndex, cbxType.SelectedIndex, 20, dpList.PageIndex + 1);
        //}

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            AddMember am = new AddMember();
            am.Show();
            am.Closed += (s, er) =>
            {
                if (am.DialogResult == true)
                {
                    loadingProgress.IsBusy = true;
                    client.GetMemberListDataAsync(am.userId, 0, 0, 0,"");
                }
            };
        }

        private void btDelete_Click(object sender, RoutedEventArgs e)
        {
            AdminService.LotteryAdminClient srv = WcfProxy.GetProxy;
            srv.DeleteMemberAsync((sender as Button).Tag as MemberList);
            loadingProgress.IsBusy = true;
            srv.DeleteMemberCompleted += (s, er) =>
            {
                loadingProgress.IsBusy = false;
                if (er.Result == false)
                {
                    MessageBox.Show("删除会员操作失败!");
                    return;
                }
                PagedCollectionView pageView=(PagedCollectionView)(dgList.ItemsSource);
                System.Collections.ObjectModel.ObservableCollection<MemberList> list 
                    = (System.Collections.ObjectModel.ObservableCollection<MemberList>)pageView.SourceCollection;
                list.Remove((sender as Button).Tag as MemberList);
            };
          
        }

        private void TextBlock_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (SetNavigater(((sender as TextBlock).Tag as MemberList)))
                return;
            loadingProgress.IsBusy = true;
            client.GetLowerMemberByUIDAsync(((sender as TextBlock).Tag as MemberList).ID);
        }

        void client_GetLowerMemberByUIDCompleted(object sender, GetLowerMemberByUIDCompletedEventArgs e)
        {
            loadingProgress.IsBusy = false;
            currentPos = 0;
            PagedCollectionView pcw = new PagedCollectionView(e.Result);
            this.dgList.ItemsSource = pcw;
            dataPager.Source = pcw;
        }
        bool SetNavigater(MemberList info)
        {
            foreach (TextBlock textBlock in sp_Navigater.Children)
            {
                if (textBlock.Tag != null && Convert.ToInt64(textBlock.Tag.ToString()) == info.ID)
                {
                    return true;
                }
            }
            TextBlock tb_UserIdNavigater = new TextBlock();
            tb_UserIdNavigater.Foreground = new SolidColorBrush(Colors.Orange);
            tb_UserIdNavigater.Text = info.UserID;
            tb_UserIdNavigater.Tag = info.ID;
            tb_UserIdNavigater.Cursor = Cursors.Hand;
            tb_UserIdNavigater.MouseLeftButtonDown += new MouseButtonEventHandler(tb_UserIdNavigater_MouseLeftButtonDown);
            TextBlock tb = new TextBlock();
            tb.Foreground = new SolidColorBrush(Colors.Gray);
            tb.Text = ">";
            if (sp_Navigater.Children.Count != 0)
                sp_Navigater.Children.Add(tb);
            sp_Navigater.Children.Add(tb_UserIdNavigater);
            return false;
        }

        void tb_UserIdNavigater_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            TextBlock txtb = sender as TextBlock;
            int index = sp_Navigater.Children.IndexOf(txtb);
            if (index != sp_Navigater.Children.Count - 1)
            {
                for (int i = sp_Navigater.Children.Count - 1; i > index; i--)
                {
                    TextBlock txtblock = sp_Navigater.Children[i] as TextBlock;
                    sp_Navigater.Children.RemoveAt(i);
                }
            }
            loadingProgress.IsBusy = true;
            client.GetLowerMemberByUIDAsync(Convert.ToInt64(txtb.Tag.ToString()));
        }

        private void btStauts_Click(object sender, RoutedEventArgs e)
        {
            client = WcfProxy.GetProxy;
            MemberList m = (sender as Button).Tag as MemberList;
            if (m.IsActive == 0)
                client.FrozenUserAsync(1, m.UserID);
            else
                client.FrozenUserAsync(0, m.UserID);
            loadingProgress.IsBusy = true;
            client.FrozenUserCompleted += (s, er) =>
            {
                loadingProgress.IsBusy = false;
                if (er.Result)
                {
                    if (m.IsActive == 0)
                    {
                        m.IsActive = 1;
                        (sender as Button).Foreground = new SolidColorBrush(Colors.Green);
                        (sender as Button).Content = "激活";
                    }
                    else
                    {
                        m.IsActive = 0;
                        (sender as Button).Foreground = new SolidColorBrush(Colors.Red);
                        (sender as Button).Content = "锁定";
                    }
                }
                else
                    MessageBox.Show("操作提交失败!网络故障或者服务器繁忙未能响应，请稍后重试!");
            };
        }
    }
}
